package day03;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Demo04 {
    public static void main(String[] args)
        throws Exception{
        /*
         * JDBC 访问数据库
         */
        //1. 注册数据库驱动
        //Class.forName("com.mysql.cj.jdbc.Driver");
        //2. 连接到数据库
        String sql = "INSERT INTO users (id, name) values (?, ?)";
        String username = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/testdb?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
        try(Connection conn = DriverManager.getConnection(
                url,username, password
        )){
            //3. 创建 Statement对象
            PreparedStatement ps = conn.prepareStatement(sql);
            //4. 执行SQL
            ps.setInt(1, 2);
            //ps.setString(2, "熊大");
            ps.setString(2, "心情\uD83D\uDE1D");
            int n = ps.executeUpdate();
            //5. 处理结果
            System.out.println(n);
        }
            //6. 关闭连接
    }
}
